package com.seamtop.cuber.common.key;

import com.seamtop.cuber.common.base.DataObject;
import com.seamtop.cuber.common.metadata.Column;
import com.seamtop.cuber.common.metadata.TableMetaData;
import com.seamtop.cuber.common.util.DateUtil;
import com.seamtop.cuber.common.util.RandomUtil;
import com.seamtop.cuber.common.util.StringUtil;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by feng on 2015/8/10.
 */
public class MatchUtil {

    public static String getKey(HashMap<String,Object> dataMap ,String keyPrefix,Long t,TableMetaData tableMetaData){
        String rowKey = null;
        String dataStr = DateUtil.getDateStr(new Date(t));
        rowKey = keyPrefix + "_" +  dataStr;
        int count = 0;
        for(String param: dataMap.keySet()){
            Column column = tableMetaData.getColumnMap().get(param);
            if(column.isPrimaryDepend()){
                String value = (String)dataMap.get(param);
                if(StringUtil.isEmpty(value)){
                    continue;
                }
                if(count == 0){
                    rowKey += "|";
                }else{
                    rowKey += "_";
                }
                count ++;
                value = value.replace(":","");
                rowKey += column.getMatch() + ":" + value;
            }
        }
        return rowKey;
    }

    public String getCarHitKey(HashMap<String,String> dataMap ,String keyPrefix,Long t){
        String key = dataMap.get("car_id");
        String dataStr = DateUtil.getDateStr(new Date(t));
        String rowKey = keyPrefix + "_" +  dataStr+"_"+key;
        return rowKey;
    }

    public String getCounterKey(HashMap<String,String> dataMap ,String keyPrefix,Long t){
        String dataStr = DateUtil.getDateStr(new Date(t));
        String rowKey = keyPrefix + "_" +  dataStr;
        return rowKey;
    }

    public String getKeyWordKey(HashMap<String,String> dataMap ,String keyPrefix,Long t){
        String key = dataMap.get("key_word");
        String dataStr = DateUtil.getDateStr(new Date());
        String rowKey = keyPrefix + "_" +  dataStr+"_"+key;
        return rowKey;
    }

    public String getKeyWordTypeKey(HashMap<String,String> dataMap ,String keyPrefix,Long t){
        String key = dataMap.get("key_word_type");
        String dateStr = DateUtil.getDateStr(new Date());
        String rowKey = keyPrefix + "_" + dateStr+"_"+key;
        return rowKey;
    }

    public String getStatAddCarKey(HashMap<String,String> dataMap ,String keyPrefix,Long t){
        String key = dataMap.get("car_id");
        String dateStr = DateUtil.getDateStr(new Date());
        String rowKey = keyPrefix + "_" + dateStr+"_"+key;
        return rowKey;
    }

    public static String generateMatch(int length){
        boolean flag = true;
        String random = null;
        while (flag){
            random = RandomUtil.randomAllChar(length);
            if(!DataObject.getMatchList().contains(random)){
                return random;
            }
        }
        return random;
    }

}
